#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import openpyxl


class MakeExcel:
    def __init__(self, file_path):
        self.file_path = file_path
        self.file_name = self.file_path.split('\\')[-1]
        self.workbook = openpyxl.load_workbook(self.file_path, data_only=True)
        self.worksheet_names = self.workbook.sheetnames

    def get_worksheet(self, sheet_name):
        try:
            return self.workbook[sheet_name]
        except KeyError:
            self.workbook.create_sheet(sheet_name)
            return self.workbook[sheet_name]

    def save_workbook(self):
        self.workbook.save(self.file_path)

    def make_comment(self, sheet_name, row, cmt_string):
        ws = self.get_worksheet(sheet_name)
        ws.cell(row, 1).value = '>>Commentary'
        ws.cell(row, 2).value = cmt_string

    def make_elements(self, sheet_name, row, element_attr_group):
        ws = self.get_worksheet(sheet_name)
        title_list, val_list, val_lists = [], [], []
        for attr_dict in element_attr_group:
            for key in attr_dict.keys():
                if key not in title_list:
                    title_list.append(key)
        for idx, title in enumerate(title_list):
            ws.cell(row, idx + 1).value = title
        row += 1
        for attr_dict in element_attr_group:
            for key, value in attr_dict.items():
                title_col = title_list.index(key) + 1
                ws.cell(row, title_col).value = value
            row += 1
        return row

if __name__ == '__main__':
    pass